$(function () {
    let markid = getUrlString('markid');
    let userId = getUrlString('userid');
    let isApp = getUrlString('isApp');
    let url = '';
    let userid= '';
    function userInfo() {
        let data;
        if (isApp === 'h5'){
            data = {
                'userid':userId
            };
            $('.inviteList,#shareBtn').hide();
            $('#helpBtn').show().click(function () {
                $('#helpWrap').show();
            });
        } else {
            data = {
                'markid':markid
            };
        }
        getData('get', '/niu/apiv4/share_user_detail.php',data, function (data) {
            console.log(data);
            $('#faceurl').attr('src',data.ShareUser.faceurl);
            $('#userName').html(data.ShareUser.name);
            userid = data.ShareUser.userid;
            url = urlView+'activity/help.html?userid='+userid+'&isApp=h5';
            if (data.surPlusGold !== 0){
                $('#surPlusGold').html(data.surPlusGold + '/' + data.allGold );
            }

            $('#totalGold').html(data.totalGold);
            let width = (data.surPlusGold/data.allGold)*100+'%';
            $('#bar').css({
                width:width
            });

            if (data.resultcode === '200'){

                $('#ShareByUserNum').html(data.ShareByUser.length);
                let append = '';
                $(data.ShareByUser).each(function () {
                    append += '<li>' +
                        '                        <div class="left">' +
                        '                            <img src="'+this.faceurl+'" alt="">' +
                        '                        </div>' +
                        '                        <div class="right">' +
                        '                            <p>'+this.name+'</p>' +
                        '                            <p>已获得'+this.share_gold+'金币红包</p>' +
                        '                        </div>' +
                        '                    </li>'
                });
                $('#ShareByUser').append(append)
            } else {
                $('#ShareByUserNum').html(0);
            }
        })
    }
    userInfo();

    $('#shareBtn').click(function () {
        $('#shareWrap').show();
    });

    $('#shareAlert').click(function () {
        $('#shareWrap').hide();
    });

    $('#indexBack').click(function () {
        $('#conAlert').hide();
    });

    $('#helpWrapAlert').click(function () {
        $('#helpWrap').hide();
    });

    $('#gotoAppBtn').click(function () {
        window.location.href = 'https://www.odancool.com'
    });

    //获取验证码
    $('#getCode').click(function () {
        getCode();
    });

    $('#loginBtn').click(function () {
        let phone = $('#phoneNum').val();
        let phoneCode = $('#phoneCode').val();
        login(phone,phoneCode)
    });

    function login (phone,pw) {
        if (phone === ''){
            laymsg('手机号不能为空');
            return
        }
        if (pw === ''){
            laymsg('验证码不能为空');
            return
        }
        let data = {
            'phone':phone,
            'pw':pw,
            'openid1':'',
            'nan':'',
        };
        getData('post', '/niu/api/member_login_phone.php', data, function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                help(phone);
                $('#phoneNum').val('');
                $('#phoneCode').val('');
                $('#helpWrap').hide();
            } else {
                laymsg(data.msg)
            }
        })
    }

    function help (phone) {
        let data = {
            'phone':phone,
            'userid':userid
        };
        getData('post', '/niu/apiv4/niu_share_user.php', data, function (data) {
            console.log(data);
            if (data.resultcode === '200'){
                // laymsg(data.msg);
                $('#conAlert').show();
            } else {
                laymsg(data.msg)
            }
        })
    }

    function getCode () {

        let phone = $('#phoneNum').val();
        if (!phoneNumberVer(phone)){
            return
        }

        let str = getCurrentTime()+phone;
        let jiaMi = btoa(str);
        let jiaMi2 = btoa(jiaMi);
        let signStr = randomWord(false,8);
        let sign = signStr + jiaMi2;
        let data = {
            'phone':phone,
            'sign':sign
        };
        getData('post', '/niu/api/member_login_smsv2.php', data, function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                $('#getCode').hide();
                $('#getCodeNum').show();
                var timer = null;
                var count = 60;
                timer = setInterval(function(){
                    count--;
                    $('#getCodeNum').html(count+'S');
                    if (count <=0) {
                        clearInterval(timer);
                        $('#getCodeNum').hide();
                        $('#getCode').show();
                    }
                },1000);
            } else {
                laymsg(data.msg)
            }
        })
    }

    /*
    ** randomWord 产生任意长度随机字母组合
    */

    function randomWord(randomFlag, min, max){
        var str = "",
            range = min,
            arr = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

        // 随机产生
        if(randomFlag){
            range = Math.round(Math.random() * (max-min)) + min;
        }
        for(var i=0; i<range; i++){
            pos = Math.round(Math.random() * (arr.length-1));
            str += arr[pos];
        }
        return str;
    }

    //获取当前时间
    function getCurrentTime() {
        var date = new Date();//当前时间
        var year = date.getFullYear();
        var month = zeroFill(date.getMonth() + 1);//月
        var day = zeroFill(date.getDate());//日
        var hour = zeroFill(date.getHours());//时
        var minute = zeroFill(date.getMinutes());//分
        var second = zeroFill(date.getSeconds());//秒

        //当前时间
        var curTime = String(year) + String(month) + String(day)+String(hour) + String(minute) + String(second);

        return curTime;
    }
    //分享到微信
    $('#shareWx').click(function () {
        
        nativeCommand('dancool://sharedjurlsingle=双11，抽手办！和我一起瓜分蛋酱10万元金币红包！|||https://api.odancool.com/weixinapp/res/img/logo.jpg|||'+url+'|||每天限量66666个金币红包，轻松助力轻松得，超值手办抽不停！|||Wechat');
    });

    //分享到朋友圈
    $('#sharePyq').click(function () {
        nativeCommand('dancool://sharedjurlsingle=双11，抽手办！和我一起瓜分蛋酱10万元金币红包！|||https://api.odancool.com/weixinapp/res/img/logo.jpg|||'+url+'|||每天限量66666个金币红包，轻松助力轻松得，超值手办抽不停！|||WechatMoments');
    });

    //分享到QQ
    $('#shareQQ').click(function () {
        nativeCommand('dancool://sharedjurlsingle=双11，抽手办！和我一起瓜分蛋酱10万元金币红包！|||https://api.odancool.com/weixinapp/res/img/logo.jpg|||'+url+'|||每天限量66666个金币红包，轻松助力轻松得，超值手办抽不停！|||QQ');
    });

    //分享到QQ空间
    $('#shareKj').click(function () {
        nativeCommand('dancool://sharedjurlsingle=双11，抽手办！和我一起瓜分蛋酱10万元金币红包！|||https://api.odancool.com/weixinapp/res/img/logo.jpg|||'+url+'|||每天限量66666个金币红包，轻松助力轻松得，超值手办抽不停！|||QZone');
    });

    //分享到微博
    $('#shareWb').click(function () {
        nativeCommand('dancool://sharedjurlsingle=双11，抽手办！和我一起瓜分蛋酱10万元金币红包！|||https://api.odancool.com/weixinapp/res/img/logo.jpg|||'+url+'|||每天限量66666个金币红包，轻松助力轻松得，超值手办抽不停！|||SinaWeibo');
    });
});